const ppp = require('calc.js')
Page({
  data:{
    num:"0",
    op:''
  },
  lastNum:0,
  isNewNum:true,
  clear(){
    this.isNewNum = true
    this.lastNum = 0
    this.setData({num:'0',op:''})
  },
  delete(){
    let num = this.data.num
    num = num.substr(0,num.length-1);//0开始,取整个num数,减掉最后一位数
    if(num==''){
      num = '0'
      this.isNewNum = true
    }
    this.setData({
      num
    })
  },
  numbtn(e){
    let val = e.currentTarget.dataset.val
    let num = this.data.num
    if(num=='0'||this.isNewNum){
      num=val
      this.isNewNum = false
    }else{
      num +=val
    }
    this.setData({
      num
    })
  },
  opbtn(e){
    console.log(e)
    let curNum = Number(this.data.num)
    let val = e.currentTarget.dataset.val
    let op = this.data.op

    this.setData({op:val})
    this.isNewNum = true
    if(this.lastNum==0){
      this.lastNum= curNum
      return 
    }
    if(op=='+'){
      this.lastNum = ppp.add(this.lastNum,curNum)
    }else if(op=='-'){
      this.lastNum = ppp.sub(this.lastNum,curNum)
    }else if(op=='*'){
      this.lastNum = ppp.mul(this.lastNum,curNum)
    }else if(op=='/'){
      this.lastNum = ppp.div(this.lastNum,curNum)
    }else if(op=='%'){
      this.lastNum %= curNum;
    }else if(op=='='){
      this.lastNum = curNum;
    }
    this.setData({
      num:this.lastNum+''
    })
  },
  dotbtn(){
    let num= this.data.num
    if(num.indexOf('.')>-1){//大于-1就代表找到了
      return
    }else{
      num += '.'
      this.setData({
        num
      })
    }
  },
  now(e){
    let v=e.currentTarget.dataset.val
    let num=this.data.num
    if(v=="pf") num=Math.pow(num,2)
    if(v=="kf") num=Math.sqrt(num)
    if(v=="sin") num=Math.sin(num)
    if(v=="cos") num=Math.cos(num)
    this.setData({num})
  }
})